//
//  Response.m
//  PatientNew
//
//  Created by xxx on 14-7-11.
//  Copyright (c) 2014年 wtf. All rights reserved.
//

#import "Response.h"

@implementation Response

+ (Response *)responseWithResult:(BOOL)result errorMsg:(NSString *)errorMsg
{
    Response *res = [[Response alloc] init];
    res.result = result;
    res.errorMsg = errorMsg;
    return res;
}

+ (Response *)responseWithData:(NSData *)data
{
    Response *res = [[Response alloc] init];
    
    res.data = data;
    res.string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSLog(@"response string: %@", res.string);
    
    id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

    res.result = [json[kResponseKeyErrCode] boolValue];
    res.msg = json[@"msg"];
//    NSLog(@" ------------------resMsg:%@",res.msg);
    if (!res.result) {
        res.errorMsg = @"error";
    } else {
        res.errorMsg = nil;
    }
    
    res.content = json[kResponseKeyContent];
    
    return res;
}

+ (Response *)responseWithData:(NSData *)data andCookies:(NSArray *)cookies
{
    Response *res = [Response responseWithData:data];
    for (NSHTTPCookie *cookie in cookies) {
        [res.cookieDict setValue:cookie forKey:cookie.name];
    }
    return res;
}

@end
